package com.gaga.console.core.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaga.common.enums.DelFlag;
import com.gaga.common.enums.MenuTypeEnum;
import com.gaga.common.tree.ForestNodeMerger;
import com.gaga.console.base.entity.PageParams;
import com.gaga.console.base.mybatisplus.LambdaQueryWrapperExt;
import com.gaga.console.base.service.impl.SuperServiceImpl;
import com.gaga.console.core.entity.MenuMeta;
import com.gaga.console.core.entity.SysMenu;
import com.gaga.console.core.entity.vo.SysMenuVO;
import com.gaga.console.core.mapper.SysMenuMapper;
import com.gaga.console.core.service.SysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author liujia
 * @since 2022-07-14
 */
@Service
public class SysMenuServiceImpl extends SuperServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    public static final String HIDE_MENU = "1";

    @Override
    public List<SysMenuVO> routes() {

        List<String> roleList = StpUtil.getRoleList();

        //1. 获取用户的菜单列表，待扩展
        List<SysMenu> menus = this.baseMapper.routes(roleList.get(0));
        //2. 生成菜单树
        return ForestNodeMerger.merge(buildTree(menus));
    }

    @Override
    public boolean checkChild(Long id) {
        return this.getMenuChild(id) > 0L ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Long getMenuChild(Long menuId) {
        return this.baseMapper.selectCount(new LambdaQueryWrapper<SysMenu>().select(SysMenu::getId).eq(SysMenu::getDelFlag, DelFlag.OK.getCode()).eq(SysMenu::getParentId, menuId));
    }

    @Override
    public List<SysMenuVO> searchList(PageParams<SysMenu> data) {
        LambdaQueryWrapperExt<SysMenu> wraps = new LambdaQueryWrapperExt<>();
        wraps.eq(SysMenu::getDelFlag, DelFlag.OK.getCode()).orderByAsc(SysMenu::getSeqNo);
        data.buildSpecWrap(wraps);
        return this.buildTree(this.baseMapper.selectList(wraps));
    }

    /**
     * 对象转树节点
     *
     * @param sysMenus 系统菜单
     * @return List
     */
    public List<SysMenuVO> buildTree(List<SysMenu> sysMenus) {
        List<SysMenuVO> trees = new ArrayList<>();
        sysMenus.forEach(sysMenu -> {
            SysMenuVO tree = new SysMenuVO();
            BeanUtils.copyProperties(sysMenu, tree);
            MenuMeta meta = new MenuMeta();
            meta.setIcon(sysMenu.getIcon());
            meta.setTitle(sysMenu.getName());
            meta.setHideMenu(HIDE_MENU.equals(sysMenu.getHidden()));
            meta.setIgnoreKeepAlive(!sysMenu.getKeepAlive().equals("1"));
            // 只有当菜单类型为目录的时候，如果是顶级，则强制修改为Layout
            if (sysMenu.getParentId() == -1L && MenuTypeEnum.DIR.getCode().equals(sysMenu.getType())) {
                tree.setComponent("Layout");
                tree.setRedirect("noRedirect");
                tree.setAlwaysShow(true);
            }
            tree.setMeta(meta);
            tree.setCreateTime(sysMenu.getCreateTime());
            if (MenuTypeEnum.DIR.getCode().equals(sysMenu.getType())) {
                tree.setTypeName(MenuTypeEnum.DIR.getMessage());
            } else if (MenuTypeEnum.MENU.getCode().equals(sysMenu.getType())) {
                tree.setTypeName(MenuTypeEnum.MENU.getMessage());
            } else if (MenuTypeEnum.BUTTON.getCode().equals(sysMenu.getType())) {
                tree.setTypeName(MenuTypeEnum.BUTTON.getMessage());
            }
            trees.add(tree);
        });
        return trees;
    }
}
